package com.example.easytraffic.core;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

public class DelayedTraffic implements Traffic{

	private long delay;
	private TimeUnit delayTimeUnit;
	private Traffic traffic;
	private Timer timer;
	
	public DelayedTraffic(Traffic traffic,
			long delay, 
			TimeUnit delayTimeUnit) {
		super();
		this.timer = new Timer();
		this.traffic = traffic;
		this.delay = delay;
		this.delayTimeUnit = delayTimeUnit;
	}

	public void run() {
		long delayInMillis = delayTimeUnit.toMillis(delay);
		timer.schedule(new TimerTask() {
			public void run() {
				traffic.run();
			}
		}, delayInMillis);
	}

	public void stop() {
		timer.cancel();
		traffic.stop();
	}
}
